﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace SuperFind.Lib.Data
{
   public class OleDbUtility
    {
        //public const string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data.mdb";
        public const string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data\Data.mdb";
        private static OleDbConnection connection = null;
        public static OleDbConnection Connection 
        { 
            get
            {
                if(connection==null)
                    connection = new OleDbConnection(ConnectionString);
                return connection;
            }
        }
        public static int TryExecuteNonQuery(string query)
        {  
           int rows = -1;
           if (Connection != null)
           {
               try
               {   
                   OleDbCommand command = new OleDbCommand(query, Connection);
                   Connection.Open();
                   rows = command.ExecuteNonQuery();
                   Connection.Close();
               }
               catch { }
           }
           return rows;
        }
        public static DataTable  TryExecuteDataTable(string query)
        {
            DataTable table = new DataTable();
            try
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter(query, Connection);
                adapter.Fill(table);
            }
            catch
            {
                table = null;
            }
            return table;
        }
    }
}
